package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.pojo.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by ZhaoJun on 2021/11/28 11:12
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ShoppingCartService cartService;

    @PostMapping("/add")
//    @Cacheable(value = "CART",key = "'list'")
    public R add(@RequestBody ShoppingCart cart){
        Long userId = BaseContext.getCurrentId();
        cart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        if(cart.getDishId()!=null){
            lqw.eq(ShoppingCart::getDishId,cart.getDishId());

        }
        if(cart.getSetmealId()!=null){
            lqw.eq(ShoppingCart::getSetmealId,cart.getSetmealId());

        }
        lqw.eq(ShoppingCart::getUserId,cart.getUserId());

        ShoppingCart one = cartService.getOne(lqw);
        if(one !=null){
            one.setNumber(one.getNumber()+1);
            cartService.updateById(one);
            return R.success("添加成功");
        }
        cartService.save(cart);

        return R.success("添加成功");
    }

    @GetMapping("/list")
    public R findList(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = cartService.list(lqw);



        return R.success(list);


    }

    @DeleteMapping("/clean")
//    @CacheEvict(value = "CART",allEntries = true)
    public R clean(){
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        cartService.remove(lqw);

        return R.success("清空成功");
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        Long userId = BaseContext.getCurrentId();




        if(dishId!=null){
            int i = 0;

            LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getUserId,userId).eq(ShoppingCart::getDishId,dishId);
            ShoppingCart one = cartService.getOne(lqw);

            one.setNumber(one.getNumber()-1);
            cartService.updateById(one);
            if(one.getNumber()==0){
                cartService.removeById(one);
            }
        }

        if(setmealId!=null){
            LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getUserId,userId).eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart one = cartService.getOne(lqw);

            one.setNumber(one.getNumber()-1);
            cartService.updateById(one);
            if(one.getNumber()==0){
                cartService.removeById(one);
            }
        }

        return R.success("减少成功");

    }










}
